package com.gitee.wsl.compose.state

import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.ime
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import com.gitee.wsl.base.Tuple2
import com.gitee.wsl.base.tuple

/**
 * Description:
 * @author Junerver
 * date: 2024/4/16-10:14
 * Email: junerver@gmail.com
 * Version: v1.0
 */
@Composable
fun S.useKeyboard(): Tuple2<() -> Unit, () -> Unit> {
    val keyboardController = LocalSoftwareKeyboardController.current
    return tuple(
        first = { keyboardController?.hide() },
        second = { keyboardController?.show() }
    )
}


@Composable
fun S.keyboardVisibility(): State<KeyboardState> {
    return rememberUpdatedState(
        if (WindowInsets.ime.getBottom(LocalDensity.current) > 0) KeyboardState.Opened
        else KeyboardState.Closed
    )
}

enum class KeyboardState {
    Opened,
    Closed
}